import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../resources.dart';

/// created by hcl
/// on 2024/02/23
/// page LoginTextField
/// desc 登录输入框
class LoginTextField extends StatefulWidget {
  TextEditingController textController;
  FocusNode focusNode;
  String hintText;
  String title;
  String image;

  int maxLines;
  int maxLength;
  bool enable;
  bool showPassword;
  bool isDark;
  bool showBorder;
  TextInputType keyboardType;
  TextAlign textAlign;
  List<TextInputFormatter>? inputFormatters;
  TextInputAction textInputAction;
  ValueChanged<String>? onChanged;

  LoginTextField(
    this.textController,
    this.focusNode,
    this.hintText, {
    super.key,
    this.title = '',
    this.image = '',
    this.maxLines = 1,
    this.maxLength = 40,
    this.enable = true,
    this.showPassword = false,
    this.isDark = true,
    this.showBorder = true,
    this.textAlign = TextAlign.start,
    this.keyboardType = TextInputType.text,
    this.inputFormatters,
    this.textInputAction = TextInputAction.done,
    this.onChanged,
  });

  @override
  State<StatefulWidget> createState() => LoginTextFieldState();
}

class LoginTextFieldState extends State<LoginTextField> {
  bool isShowPassword = false;

  @override
  initState() {
    super.initState();
    if (widget.showPassword) {
      isShowPassword = true;
      if (mounted) setState(() {});
    }
  }

  showPasswordChange() {
    isShowPassword = !isShowPassword;
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 48.ss,
      padding: EdgeInsets.only(
          left: widget.image.isNotEmpty ? 5.ss : 20.ss, right: 5.ss),
      decoration: widget.showBorder
          ? BoxDecoration(
              color: widget.isDark
                  ? ColorUtils.color_FFFFFF
                  : ColorUtils.color_F5F5F5,
              border: Border.all(
                  color: widget.isDark
                      ? ColorUtils.color_FFFFFF
                      : Colors.transparent),
              borderRadius: BorderRadius.circular(16.ss))
          : null,
      child: Row(
        children: [
          widget.image.isNotEmpty
              ? Container(
                  width: 40.ss,
                  height: 40.ss,
                  padding: EdgeInsets.all(8.ss),
                  child: assetImage(widget.image, fit: BoxFit.contain))
              : const SizedBox(),
          widget.title.isNotEmpty
              ? Text(widget.title, style: normal012)
              : const SizedBox(),
          Expanded(child: _buildTextField()),
          _buildShowPassword(),
        ],
      ),
    );
  }

  Widget _buildTextField() {
    return TextField(
      maxLines: widget.maxLines,
      maxLength: widget.maxLength,
      controller: widget.textController,
      focusNode: widget.focusNode,
      enabled: widget.enable,
      keyboardType: widget.keyboardType,
      obscureText: isShowPassword,
      textAlign: widget.textAlign,
      textInputAction: widget.textInputAction,
      // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
      inputFormatters: getInputFormatter(),
      style: widget.isDark ? medium014 : normal014,
      decoration: InputDecoration(
        counterText: '',
        hintText: widget.hintText,
        hintStyle: widget.isDark ? normal714 : normalB114,
        contentPadding: const EdgeInsets.all(0),
        filled: true,
        fillColor: Colors.transparent,
        border: _getBorder(),
        enabledBorder: _getBorder(),
        disabledBorder: _getBorder(),
        focusedBorder: _getBorder(),
      ),
      onChanged: widget.onChanged,
    );
  }

  List<TextInputFormatter>? getInputFormatter() {
    if (widget.inputFormatters != null) {
      return widget.inputFormatters!;
    } else {
      List<TextInputFormatter> tempList = [];
      if (widget.keyboardType == TextInputType.number ||
          widget.keyboardType == TextInputType.phone) {
        tempList.add(FilteringTextInputFormatter.digitsOnly);
      }
      return tempList;

      ///如果展示密码
      // if (needPassword) {
      //   tempList
      //       .add(FilteringTextInputFormatter.allow(RegExp("[\u4e00-\u9fa5]")));
      // }
    }
  }

  OutlineInputBorder _getBorder() {
    return const OutlineInputBorder(
      borderSide: BorderSide(
        color: Colors.transparent,
      ),
    );
  }

  Widget _buildShowPassword() {
    return widget.showPassword
        ? InkWell(
            onTap: () {
              showPasswordChange();
            },
            child: Container(
              width: 40.ss,
              height: 40.ss,
              alignment: Alignment.center,
              child: assetImage(
                  isShowPassword
                      ? ImageUtils.pwd_not_show
                      : ImageUtils.pwd_show,
                  width: 18.ss,
                  height: 16.ss),
            ),
          )
        : const SizedBox();
  }
}
